package com.itheima.media.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.pojo.PageRequestDto;
import com.itheima.common.pojo.Result;
import com.itheima.core.controller.AbstractCoreController;
import com.itheima.media.dto.WmNewsDto;
import com.itheima.media.dto.WmNewsDtoSave;
import com.itheima.media.dto.WmNewsVo;
import com.itheima.media.pojo.WmNews;
import com.itheima.media.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
* <p>
* 自媒体图文内容信息表 控制器</p>
* @author 作者
* @since 2021-08-20
*/
@RestController
@RequestMapping("/wmNews")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    //注入
    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }


    /***
     * 文章详情  WmNews->WmNewsVo
     */
    @GetMapping(value = "/detail/{id}")
    public Result<WmNewsVo> getDeatilById(@PathVariable(value = "id")Integer id){
        WmNewsVo wmNewsVo = wmNewsService.findDetailById(id);
        return Result.ok(wmNewsVo);
    }


    /***
     * 人工审核
     *  审核->2:审核失败   8：定时发布     9：发布
     */
    @PutMapping(value = "/{id}/{status}")
    public Result verify(@PathVariable(value = "id")Integer id,@PathVariable(value = "status")Integer status){
        //人工审核
        wmNewsService.verify(id,status);
        return Result.ok();
    }

    /***
     * 条件分页列表查询
     * @param pageRequestDto
     * @return
     */
    @PostMapping("/vo/search")
    public Result<PageInfo<WmNewsVo>> searchByCondition(@RequestBody PageRequestDto<WmNews> pageRequestDto){
        PageInfo<WmNewsVo> pageInfo = wmNewsService.pageForCondition(pageRequestDto);
        return Result.ok(pageInfo);
    }

    /****
     * 上架、下架
     */
    @PutMapping(value = "/upOrDown/{id}/{enable}")
    public Result updateUpDown(@PathVariable(value = "id")Integer id,@PathVariable("enable")Integer enable){
        int count = wmNewsService.updateUpDown(id,enable);
        if(count>0){
            return Result.ok();
        }
        return Result.errorMessage("操作失败！");
    }

    /***
     * 分页条件查询文章列表数据
     */
    @PostMapping(value = "/searchPage")
    public Result pageList(@RequestBody PageRequestDto<WmNewsDto> dto){
        //1：创建分页对象
        Page<WmNews> page = new Page<WmNews>(dto.getPage(),dto.getSize());

        //2：查询条件封装->QueryWrapper<WmNews>
        WmNewsDto wmNewsDto = dto.getBody();
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<WmNews>();
        if(wmNewsDto!=null){
            //状态查询
            if(!StringUtils.isEmpty(wmNewsDto.getStatus())){
                queryWrapper.eq("status",wmNewsDto.getStatus());
            }
            //关键词查询（标题模糊查询）
            if(!StringUtils.isEmpty(wmNewsDto.getTitle())){
                queryWrapper.like("title",wmNewsDto.getTitle());
            }
            //频道查询
            if(!StringUtils.isEmpty(wmNewsDto.getChannelId())){
                queryWrapper.eq("channel_id",wmNewsDto.getChannelId());
            }
            //发布日期查询（区间查询）
            if(!StringUtils.isEmpty(wmNewsDto.getStartTime()) && !StringUtils.isEmpty(wmNewsDto.getEndTime())){
                queryWrapper.between("created_time",wmNewsDto.getStartTime(),wmNewsDto.getEndTime());
            }
        }
        //3：执行分页查询
        IPage<WmNews> iPage = wmNewsService.page(page,queryWrapper);

        //4：封装分页响应结果对象PageInfo<T>
        PageInfo<WmNews> pageInfo = new PageInfo<WmNews>(iPage.getCurrent(),iPage.getSize(),iPage.getTotal(),iPage.getPages(),iPage.getRecords());
        return Result.ok(pageInfo);
    }


    /***
     * 文章删除
     */
    @Override
    @DeleteMapping(value = "/{id}")
    public Result deleteById(@PathVariable(value = "id") Serializable id){
        int count = wmNewsService.deleteById(id);
        if(count>0){
            return Result.ok();
        }
        return Result.errorMessage("系统繁忙，请稍后再试！");
    }

    /***
     * 保存文章
     */
    @PostMapping(value = "/save")
    public Result save(@RequestBody WmNewsDtoSave dtoSave){
        wmNewsService.save(dtoSave);
        return Result.ok();
    }

    /***
     * 根据ID查询文章详情
     */
    @GetMapping(value = "/one/{id}")
    public Result getById(@PathVariable(value = "id")Integer id){
        //根据ID查询
        WmNewsDtoSave wmNewsDtoSave = wmNewsService.getOneById(id);
        return Result.ok(wmNewsDtoSave);
    }
}

